﻿/*	
	stencil - action script 3 isometric game engine.

	http://code.google.com/p/stencilas3/
	
	Copyright (c) 2010 Max Zhuravkov, All Rights Reserved.

	Permission is hereby granted, free of charge, to any person obtaining a copy of
	this software and associated documentation files (the "Software"), to deal in
	the Software without restriction, including without limitation the rights to
	use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
	of the Software, and to permit persons to whom the Software is furnished to do
	so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in all
	copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
	SOFTWARE.
*/
package stencil.core.engine {
	
	import flash.events.EventDispatcher;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	import flash.utils.getQualifiedClassName;
	import stencil.core.engine.errors.ComponentError;
	import stencil.core.engine.IComponent;
	import stencil.core.errors.ArgumentIsNullError;
	import stencil.core.errors.NotImplementedMethodError;
	import stencil.core.engine.Bounds;
	import stencil.core.engine.Box;
	import stencil.core.engine.errors.IllegalStateError;
	import stencil.core.engine.events.ComponentEvent;
	import stencil.core.engine.utils.getClassName;

	
	/**
	 * An imlemenation of IGameObject interface which provides all basic functionality. 
	 * Other game object types should derive from class one rather then implementing IGameObject interface by its own.
	 * 
	 * @author Max Zhuravkov
	 */
	public class GameObject extends AbstractComponent implements IGameObject{
		
		private static const classes:Object = { };
		
		private var _id:String;
		
		private var _type:String;
		
		private var _generateId:Boolean;
		
		private var _position:Position;
		
		private var _dimensions:Box = new Box(0, 0, 0, 1, 1, 1);
		
		private var _bounds:Bounds;
		
		private var _componentList:IComponentList;
		
		private var _disposed:Boolean;
		
		private var _flagDisplayable:Boolean;
		
		private var _flagStatic:Boolean;
		
		private var _flagBackground:Boolean;
		
		//------------------------------------------------------------------------------------------------
		// Constructor
		//------------------------------------------------------------------------------------------------
		
		public function GameObject(generateId:Boolean = false) {
			super();
			
			this._generateId = generateId;
			this._componentList = new ComponentList();
			this._componentList.owner = this;
			this._componentList.addEventListener(ComponentEvent.STN_ADD_COMPONENT, this.addComponentHandler);
			this._componentList.addEventListener(ComponentEvent.STN_REMOVE_COMPONENT, this.removeComponentHandler);
			this.init();
		}
		
		//------------------------------------------------------------------------------------------------
		// Public properties
		//------------------------------------------------------------------------------------------------
		
		/**
		 * @inheritDoc
		 */
		override public function set id(value:String):void {
			if (this._id == value) {
				return;
			}
			this._id = value;
		}
		
		override public function get id():String {
			if (!this._id && this._generateId) {
				this._id = GameObject.generateID(this);
			}
			
			return this._id;
		}
		
		/**
		 * @private
		 */
		public function set type(value:String):void {
			this._type = value;
		}
		
		/**
		 * @inheritDoc
		 */
		public function get type():String {
			return this._type;
		}
		
		/**
		 * @inheritDoc
		 */
		public final function set position(value:Position):void {
			this.checkState();
			if (!value) {
				throw new ArgumentIsNullError("position");
			}
			if (value.equals(this._position)) {
				return;
			}
			if (!this.position) {
				this._position = new Position();
			}
			this._position.x = value.x;
			this._position.y = value.y;
			this._position.z = value.z;
		}
		
		public final function get position():Position {
			return this._position;
		}
		
		/**
		/**
		 * @inheritDoc
		 */
		public final function set dimensions(value:Box):void {
			this.checkState();
			if (!value) {
				throw new ArgumentIsNullError("dimensions");
			}
			if (value.sizesEqual(this._dimensions)) {
				return;
			}
			this._dimensions.dx = value.dx;
			this._dimensions.dz = value.dy;
			this._dimensions.dy = value.dz;
			
			this.dimensionsChanged(this._dimensions);
		}
		
		public final function get dimensions():Box {
			return this._dimensions;
		}
		
		/**
		 * @inheritDoc
		 */
		public final function set bounds(value:Bounds):void {
			this.checkState();
			if (!value) {
				throw new ArgumentIsNullError("bounds");
			}
			if (value.equals(this._bounds)) {
				return;
			}
			if(this._bounds){
				this._bounds.setTo(value.rect);
			}else {
				this._bounds = new Bounds(value.rect);
			}
			this.boundsChanged(value);
		}
		
		public final function get bounds():Bounds {
			return this._bounds;
		}
		
		/**
		 * Serialization support.
		 * @private
		 */
		public final function set components(value:Array):void {
			this._componentList.components = value;
		}
		
		/**
		 * @inheritDoc
		 */
		public final function get components():Array {
			return this._componentList.components;
		}
		
		public function set flagDislayable(value:Boolean):void {
			if (this._flagDisplayable == value) {
				return;
			}
			this._flagDisplayable = value;
		}
		
		/**
		 * @inheritDoc
		 */
		public function get flagDisplayable():Boolean {
			return this._flagDisplayable;
		}
		
		public function set flagStatic(value:Boolean):void {
			if (this._flagStatic == value) {
				return;
			}
			this._flagStatic = value;
		}
		
		/**
		 * @inheritDoc
		 */
		public function get flagStatic():Boolean {
			return this._flagStatic;
		}
		
		/**
		 * @inheritDoc
		 */
		public final function get flagTile():Boolean {
			return false;
		}
		
		/**
		 * @inheritDoc
		 */
		public function set flagBackground(value:Boolean):void {
			if (this._flagBackground == value) {
				return;
			}
			this._flagBackground = value;
		}
		
		public function get flagBackground():Boolean {
			return this._flagBackground;
		}
		
		override public function toString():String {
			return "[" + this.id + " position=" +  this.position +  "]";
		}
		
		//------------------------------------------------------------------------------------------------
		// Public methods
		//------------------------------------------------------------------------------------------------
		
		/**
		 * @inheritDoc
		 */
		public function addComponent(id:String, component:IComponent, data:Object = null):IComponent {
			return this.componentList.addComponent(id, component, data);
		}
		/**
		 * @inheritDoc
		 */
		public function addComponentType(id:String, componentType:Class, data:Object = null):IComponent {
			return this.componentList.addComponentType(id, componentType,data);
		}
		
		/**
		 * @inheritDoc
		 */
		public function getComponentByType(componentType:Class):IComponent {
			return this.componentList.getComponentByType(componentType);
		}
		
		/**
		 * @inheritDoc
		 */
		public function getComponentsByType(componentType:Class):Array {
			return this.componentList.getComponentsByType(componentType);
		}
		
		/**
		 * @inheritDoc
		 */
		public function getComponentById(id:String):IComponent {
			return this.componentList.getComponentById(id);
		}
		
		/**
		 * @inheritDoc
		 */
		public function containsComponentType(componentType:Class):Boolean {
			return this.componentList.containsComponentType(componentType);
		}
		
		/**
		 * @inheritDoc
		 */
		public function containsComponent(component:IComponent):Boolean {
			return this.componentList.containsComponent(component);
		}
		
		/**
		 * @inheritDoc
		 */
		public function removeComponent(component:IComponent):IComponent {
			return this.componentList.removeComponent(component);
		}
		
		/**
		 * @inheritDoc
		 */
		public function removeComponentsByType(componentType:Class):Array {
			return this.componentList.removeComponentsByType(componentType);
		}
		
		/**
		 * @inheritDoc
		 */
		public function removeComponentById(id:String):IComponent {
			return this.componentList.removeComponentById(id);
		}
		
		/**
		 * @inheritDoc
		 */
		public function removeAllComponents():Array {
			return this.componentList.removeAllComponents();
		}
			
		/**
		 * @inheritDoc
		 */
		public final function update(time:Number):void {
			this.checkState();
			this.updateComponents(time);
		}
		
		//------------------------------------------------------------------------------------------------
		// Protected properties
		//------------------------------------------------------------------------------------------------
		
		protected function get componentList():IComponentList {
			return this._componentList;
		}
		
		//------------------------------------------------------------------------------------------------
		// Protected methods
		//------------------------------------------------------------------------------------------------
		
		protected function init():void {
			
		}
		
		protected function updateComponents(time:Number):void {
			for each(var component:IComponent in this.components) {
				this.updateComponent(component, time);
			}
		}
		
		/**
		 * Updates	the component
		 * 
		 * @param	component
		 * @param	time
		 */
		protected function updateComponent(component:IComponent, time:Number):void {
			
		}
		
		/**
		 * @inheritDoc
		 */
		override protected function ownerAttached(owner:ISceneObject):void {
			if (!(owner is IGameObject)) {
				var msg:String = " component's owner can not be set. found type '" + getClassName(owner) + "' but 'IGameObject' is expected.";
				
				throw new ComponentError(this.id, msg);
			}
			
		}
		
		/**
		 * Called when bounds of this game object is changed.
		 * 
		 * @param	bounds	new bounds.
		 */
		protected function boundsChanged(bounds:Bounds):void {
			
		}
		
		/**
		 * Called when dimensions of this game object is changed.
		 * 
		 * @param	dimensions	new dimensions.
		 */
		protected function dimensionsChanged(dimensions:Box):void {
			
		}
		
		/**
		 * Add component event listener.
		 * 
		 * @param	event	the event object.
		 */
		protected function addComponentHandler(event:ComponentEvent):void {
			
		}
		
		/**
		 * Remove component event listener.
		 * 
		 * @param	event	the event object.
		 */
		protected function removeComponentHandler(event:ComponentEvent):void {
			
		}
		
		//------------------------------------------------------------------------------------------------
		// Private methods
		//------------------------------------------------------------------------------------------------
		
		private static function generateID(object:Object):String {
			var className:String = getClassName(object);
			
			var id:int = GameObject.classes[className];
			if (!isNaN(id)){
				id ++;
			}else{
				id = 0;
			}
			GameObject.classes[className] = id;
			return className + "#" + id;
		}
		
		
		
		
	}
}